package br.com.fiap.atividade3.pattern;

import java.util.HashMap;
import java.util.Map;

public aspect SingletonAOP {
	private Map<String, Singleton> instances = new HashMap<String, Singleton>();

	pointcut create(): call(Singleton+.new(..));

	Object around(): create() {
		String key = thisJoinPointStaticPart.toString();

		Singleton instance = instances.get(key);

		if (instance == null) {
			instance = (Singleton) proceed();
			System.out.println("chave do mapa: " + key);
			System.out.println("objeto do mapa: " + instance);
			instances.put(key, instance);
		}
		
		return instance;
	}
}
